/***************************************************************************
 * Copyright (C) 2005 Global Biodiversity Information Facility Secretariat.
 * All Rights Reserved.
 *
 * The contents of this file are subject to the Mozilla Public
 * License Version 1.1 (the "License"); you may not use this file
 * except in compliance with the License. You may obtain a copy of
 * the License at http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS
 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * rights and limitations under the License.
 ***************************************************************************/
package org.gbif.wdpa.service.impl;

import java.util.List;
import java.util.Map;
import java.util.HashMap;

import org.gbif.wdpa.dao.WDPADao;
import org.gbif.wdpa.model.*;
import org.gbif.wdpa.service.WDPAManager;

/**
 * Example implementation
 * @author timrobertson
 */
public class WDPAManagerImpl implements WDPAManager {
	protected WDPADao wdpaDao;
	
	/**
	 * @see org.gbif.wdpa.service.WDPAManager#getAreaDataProviders()
	 */
	public List<DataProvider> getAreaDataProviders(int areaId) {
		
		return wdpaDao.getAreaDataProviders(areaId);
	}

	/**
	 * @see org.gbif.wdpa.service.WDPAManager#getAreaTaxaStats()
	 */	
	public Map<String,Object> getAreaTaxaStats(int areaId) {
		return wdpaDao.getAreaTaxaStats(areaId);
	}

	/**
	 * @see org.gbif.wdpa.service.WDPAManager#getAreasByIds()
	 */	
	@SuppressWarnings("unchecked")
	public List getAreasByIds(Integer [] areaIds) {
		return wdpaDao.getAreasByIds(areaIds);	
	}
	
	/**
	 * @see org.gbif.wdpa.service.WDPAManager#getAreasByCoordinates()
	 */		
	@SuppressWarnings("unchecked")
	public List getAreasByCoordinates(float lat,float lng) {
		return wdpaDao.getAreasByCoordinates(lat, lng);		
	}
	
	/**
	 * @see org.gbif.wdpa.service.WDPAManager#getAreasBasicListByCountry()
	 */	
	@SuppressWarnings("unchecked")
	public Map<String,Object> getAreasBasicListByCountry(String isoCountry) {
		Map<String,Object> result = new HashMap<String,Object>();
				
		List<Map> areaData = wdpaDao.getAreasBasicData(isoCountry);
		result.put("areaData", areaData);
		
		Integer pos = 1;
		StringBuffer sb = new StringBuffer();
		for (Map<String,Object> area : areaData) {
			sb.append((String)area.get("name"));
			sb.append("<");
			sb.append(pos.toString());
			sb.append(">");
			pos++;
		};

		result.put("index", sb.toString());
		return result;		
	}

	/**
	 * @see org.gbif.wdpa.service.WDPAManager#getTileTaxonomy()
	 */		
	@SuppressWarnings("unchecked")
	public  Map<String,Object> getTileTaxonomy(int x,int y, int zoom, int areaId) {
		return wdpaDao.getTileTaxonomy(x,y,zoom,areaId);
	}	

	/**
	 * @see org.gbif.wdpa.service.WDPAManager#getAreaCellsByTileArea()
	 */		
	@SuppressWarnings("unchecked")
	public Map<String,Object> getAreaCellsByTileArea(Integer tileX,Integer tileY, Integer zoom, Integer areaId) {
		Map<String,Object> result = new HashMap();
		Map<String,Object> params = new HashMap();
		params.put("tileX", tileX);
		params.put("tileY", tileY);
		params.put("zoom", zoom);
		
		params.put("tileLimits", wdpaDao.getTileLimits(zoom,areaId));
		result.put("params", params);
		result.put("grids",wdpaDao.getAreaCellsByTileArea(tileX,tileY,zoom,areaId));
		return result;		
	}
	
	
	//--------------------------------------------------------------------------------------------------
	//DAO Accessors....
	public WDPADao getWdpaDao() {
		return wdpaDao;
	}
	/**
	 * @param wdpaDao the wdpaDao to set
	 */
	public void setWdpaDao(WDPADao wdpaDao) {
		this.wdpaDao = wdpaDao;
	}
	
}
